/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.datawander.shemareader;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Marcin Stachniuk
 */
public class MetaClass {

    private String className;
    private List<MetaClassField> fields = new ArrayList<MetaClassField>();

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public void addField(MetaClassField field) {
        fields.add(field);
    }

    public MetaClassField getField(int index) {
        return fields.get(index);
    }

    public int getFieldsNumber() {
        return fields.size();
    }

    /**
     * Return number of primary keys i MetaClass
     * @return
     */
    public int getNumberOfPrimaryKeys() {
        int counter = 0;
        for(MetaClassField f : fields) {
            if(f.isPrimaryKey()) {
                counter++;
            }
        }
        return counter;
    }

    /**
     * Test is this MetaClass association Class. MetaClass is associacion class
     * if it have 2 fields and that fields are foreign keys.
     * @return true if this MetaClass is associacion class, false otherwise.
     */
    public boolean isAssociationClass() {
        if(fields.size() == 2) {
            for(MetaClassField f : fields) {
                if(!f.isForeignKey()) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /**
     * Test is this MetaClass special associacion class to one another class.
     * @return True, if class is associacion clss and if two fields refer to
     * the same table, false otherwise.
     */
    public boolean isToOneClassAssociacion() {
        if(isAssociationClass()) {
            if(! fields.get(0).getForeignFieldTable().equals(fields.get(1).getForeignFieldTable())) {
                return false;
            }
            return true;
        }
        return false;
    }
	
}
